package com.ckzp.jfinal.weixin.util;

import com.ckzp.core.Results;
import com.ckzp.core.model.Datas;
import com.ckzp.core.util.StringUtil;
import com.ckzp.jfinal.weixin.datas.WxMessageData;
import com.ckzp.jfinal.weixin.model.WxMessage;
import com.ckzp.weixin.WxTool;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by 51594 on 2021/6/2.
 */
public class WxMessageUtil {

    public static WxMessageUtil n() {
        return new WxMessageUtil();
    }


    /**
     * 自动群发微信消息
     */
    public static synchronized void sendWxMessage() {
        List msglist = WxMessage.dao.find("select * from wx_message where status=0 and create_date<? limit 50", new Date());
        for (int k = 0; k < msglist.size(); k++) {
            WxMessage msg = (WxMessage) msglist.get(k);
            int key_id = msg.mod.getInt("key_id");
            Datas datas = Datas.n();
            Map send_result = null;
            String tempid = msg.mod.getStr("temp_id", "");
            String openid = msg.mod.getStr("open_id", "");
            datas.put("urls", msg.mod.getStr("msg_urls", ""));
            datas.put("first", msg.mod.getStr("msg_first", ""));
            datas.put("keyword1", msg.mod.getStr("msg_key1", ""));
            datas.put("keyword2", msg.mod.getStr("msg_key2", ""));
            datas.put("keyword3", msg.mod.getStr("msg_key3", ""));
            datas.put("keyword4", msg.mod.getStr("msg_key4", ""));
            datas.put("remark", msg.mod.getStr("msg_remark", ""));
            if (key_id == 2) {
                send_result = WxMessageUtil.n().sendWxMessage2(tempid, openid, datas);
            } else {
                send_result = WxMessageUtil.n().sendWxMessage(tempid, openid, datas);
            }
            String errmsg = StringUtil.getString(send_result, "errmsg");
            boolean sends = "ok".equals(errmsg);
            if (sends) {
                msg.set("status", 1).set("send_date", new Date()).set("send_desc", errmsg).update();
            } else {
                msg.set("status", 2).set("send_date", new Date()).set("send_desc", errmsg).update();
            }
        }
    }


    /**
     * 自动群发微信消息
     */
    public static boolean sendWxMessage(int ids) {
        WxMessage msg = WxMessage.dao.findById(ids);
        if (msg != null) {
            int key_id = msg.getInt("key_id");
            Datas datas = Datas.n();
            String tempid = msg.mod.getStr("temp_id", "");
            String openid = msg.mod.getStr("open_id", "");
            datas.put("urls", msg.mod.getStr("msg_urls", ""));
            datas.put("first", msg.mod.getStr("msg_first", ""));
            datas.put("keyword1", msg.mod.getStr("msg_key1", ""));
            datas.put("keyword2", msg.mod.getStr("msg_key2", ""));
            datas.put("keyword3", msg.mod.getStr("msg_key3", ""));
            datas.put("keyword4", msg.mod.getStr("msg_key4", ""));
            datas.put("remark", msg.mod.getStr("msg_remark", ""));
            Map send_result;
            if (key_id == 2) {
                send_result = WxMessageUtil.n().sendWxMessage2(tempid, openid, datas);
            } else {
                send_result = WxMessageUtil.n().sendWxMessage(tempid, openid, datas);
            }
            String errmsg = StringUtil.getString(send_result, "errmsg");
            boolean sends = "ok".equals(errmsg);
            if (sends) {
                msg.set("status", 1).set("send_date", new Date()).set("send_desc", errmsg).update();
                return true;
            } else {
                msg.set("status", 2).set("send_date", new Date()).set("send_desc", errmsg).update();
            }
        }
        return false;
    }


    /**
     * 发送模板消息
     *
     * @param openid
     */
    public Map sendWxMessage(String tempid, String openid, Datas datas) {
        String urls = datas.getStr("urls");
        datas.put("first", WxTool.getDataMap(datas.getStr("first"), "#f03c19"));
        datas.put("keyword1", WxTool.getDataMap(datas.getStr("keyword1"), "#000000"));
        datas.put("keyword2", WxTool.getDataMap(datas.getStr("keyword2"), "#000000"));
        datas.put("keyword3", WxTool.getDataMap(datas.getStr("keyword3"), "#000000"));
        datas.put("keyword4", WxTool.getDataMap(datas.getStr("keyword4"), "#000000"));
        datas.put("remark", WxTool.getDataMap(datas.getStr("remark"), "red"));
        return null;// WeixinTools.sendWeixinMessage(tempid, openid, datas, urls);
    }

    /**
     * 发送模板消息
     *
     * @param openid
     */
    public Map sendWxMessage2(String tempid, String openid, Datas datas) {
        String urls = datas.getStr("urls");
        datas.put("first", WxTool.getDataMap(datas.getStr("first"), "#f03c19"));
        datas.put("keyword1", WxTool.getDataMap(datas.getStr("keyword1"), "#000000"));
        datas.put("keyword2", WxTool.getDataMap(datas.getStr("keyword2"), "#000000"));
        datas.put("keyword3", WxTool.getDataMap(datas.getStr("keyword3"), "#000000"));
        datas.put("keyword4", WxTool.getDataMap(datas.getStr("keyword4"), "#000000"));
        datas.put("remark", WxTool.getDataMap(datas.getStr("remark"), "#000000"));
        return WxTool.sendWeixinMessage2(tempid, openid, datas, urls);
    }

    /**
     * @param datas
     * @return
     */
    public boolean addKeyMsgToPool(Datas datas) {
        int uid = datas.getInt("u_id", 0);
        int keyid = datas.getInt("key_id", 0);
        WxMessage msg = WxMessage.dao.findFirst("select * from wx_message where u_id=? and key_id=?", uid, keyid);
        if (msg == null) {
            msg = new WxMessage();
            msg.set("msg_first", datas.getStr("first"))
                    .set("msg_key1", datas.getStr("key1"))
                    .set("msg_key2", datas.getStr("key2"))
                    .set("msg_key3", datas.getStr("key3"))
                    .set("msg_key4", datas.getStr("key4"))
                    .set("msg_remark", datas.getStr("remark"))
                    .set("msg_urls", datas.getStr("urls"))
                    .set("temp_id", datas.getStr("tempid"))
                    .set("open_id", datas.getStr("openid"))
                    .set("key_id", datas.getInt("keyid"))
                    .set("u_id", datas.getInt("uid"))
                    .set("create_date", new Date());
            return msg.save();
        }
        return false;
    }


    /**
     * @param md
     * @return
     */
    public Results addToPool(WxMessageData md) {
        try {
            WxMessage msg = new WxMessage();
            msg.set("msg_first", md.getMsg_first())
                    .set("msg_key1", md.getMsg_key1())
                    .set("msg_key2", md.getMsg_key2())
                    .set("msg_key3", md.getMsg_key3())
                    .set("msg_key4", md.getMsg_key4())
                    .set("msg_remark", md.getMsg_remark())
                    .set("msg_urls", md.getMsg_urls())
                    .set("temp_id", md.getTemp_id())
                    .set("open_id", md.getOpen_id())
                    .set("key_id", md.getKey_id())
                    .set("u_id", md.getU_id())
                    .set("plan_date", md.getPlan_date())
                    .set("create_date", new Date());
            if (msg.save()) {
                return Results.n().setMsg("已提交至发送池");
            } else {
                return Results.n().setCode(9).setMsg("提交至发送池失败");
            }
        } catch (Exception e) {
            return Results.n().setCode(9).setMsg(e.getMessage());
        }
    }
}
